<?php

class App_Model_DbTable_Article_Desc extends Ext_Db_Table  
{  
    /** Table name */  
    protected $_name = 'article_desc';
    
    private static $_articleDescRow = array();
    
    public function getRowByArticleId($articleId)
    {
        if(!$articleId)
        {
            return "";
        }
        
        if(isset(self::$_articleDescRow['articleId'][$articleId]))
        {
            return self::$_articleDescRow['articleId'][$articleId];
        }
        
        $row = $this->fetchRow(" id = '".$articleId."' ");
        if (!$row) {
            return "";
        }
        $_articleDescRow = $row->toArray();
        
        self::$_articleDescRow['articleId'][$articleId] = $_articleDescRow;
        
        return $_articleDescRow;
    }
    
    public function add($inputArray, $articleId)
    {
        $_articleDesc = $this->getRowByArticleId($articleId);
        if($_articleDesc)
        {
            $sql_data_array = array();
            if(array_key_exists('content', $inputArray)) $sql_data_array['content'] = $inputArray['content'];
            if(array_key_exists('htmlOn', $inputArray)) $sql_data_array['htmlOn'] = $inputArray['htmlOn'];    
            try {
                $db = $this->getAdapter();
                $where = $db->quoteInto(" id = ? ", $articleId);
                $rows_affected = $this->update($sql_data_array, $where);
            }catch (Exception $e){
                trigger_error($e->getMessage(), E_USER_WARNING);
            }
        }
        else
        {
            $row = array (
                'id'                 => $articleId,
                'content'             => $inputArray['content'],
                'htmlOn'             => $inputArray['htmlOn'],
            );
            try {
                // 插入数据行并返回插入的行数
                $rows_affected = $this->insert($row);
            }catch (Exception $e){
                trigger_error($e->getMessage(), E_USER_WARNING);
            }
        }
        
        return "";
    }
}  

